import { message } from "ant-design-vue";

const showMessage = Symbol("showMessage");

export interface MessageApi {
  info: "info";
  success: "success";
  warning: "warning";
  error: "error";
}

const MessageInfo = () => {
  return {
    success(options: any, duration = 0.5, single = true) {
      this[showMessage]("success", options, duration, single);
    },
    warning(options: any, duration = 1, single = true) {
      this[showMessage]("warning", options, duration, single);
    },
    info(options: any, duration = 1, single = true) {
      this[showMessage]("info", options, duration, single);
    },
    error(options: any, duration = 1, single = true) {
      this[showMessage]("error", options, duration, single);
    },

    [showMessage](
      type: keyof MessageApi,
      options: any,
      duration: number,
      single: boolean
    ) {
      if (single) {
        const msgList = document.getElementsByClassName("ant-message-notice");
        // 判断是否已存在Message
        if (msgList.length === 0) {
          message[type](options, duration);
        }
      } else {
        message[type](options, duration);
      }
    },
  };
};

export const Message = MessageInfo();
